package com.lcxw.hxzbapi.controller.vipcontroller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcxw.hxzbapi.entity.pojo.GoodName;
import com.lcxw.hxzbapi.entity.pojo.Paging;
import com.lcxw.hxzbapi.entity.pojo.VipConfig;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.pojo.YwpdRoom;
import com.lcxw.hxzbapi.entity.requestBean.GetGoodNameReq;
import com.lcxw.hxzbapi.entity.requestBean.PublishPagingReq;
import com.lcxw.hxzbapi.entity.requestBean.RequestParamBasic;
import com.lcxw.hxzbapi.entity.response.GetPagingResp;
import com.lcxw.hxzbapi.entity.response.GetVipConfigRsp;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.GoodNameService;
import com.lcxw.hxzbapi.service.PagingService;
import com.lcxw.hxzbapi.service.VipConfigService;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import com.lcxw.hxzbcommon.common.DateUtils;
import com.lcxw.hxzbcommon.common.RedisUtils;
import com.lcxw.hxzbcommon.common.RoomUtils;
import com.lcxw.hxzbcommon.common.StringUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import com.netflix.ribbon.proxy.annotation.ResourceGroup;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class VipRealization {

    @Resource
    GoodNameService goodNameService;

    @Resource
    VipConfigService vipConfigService;

    @Resource
    YwpdMemberService ywpdMemberService;

    @Resource
    YwpdRoomService ywpdRoomService;

    @Resource
    PagingService pagingService;

    Response getVipConfigs(JSONObject requestBody) {
        List<VipConfig> vipConfigs = vipConfigService.getAll();
        List<GetVipConfigRsp> vipConfigRsps = new ArrayList<>();
        for (VipConfig vipconfig : vipConfigs) {
            GetVipConfigRsp getVipConfigRsp = new GetVipConfigRsp();
            String[] rights = vipconfig.getRight().split(",");
            getVipConfigRsp.setId(vipconfig.getId());
            getVipConfigRsp.setVipType(vipconfig.getVipType());
            getVipConfigRsp.setRmb(vipconfig.getRmb());
            getVipConfigRsp.setDiscount(vipconfig.getDiscount());
            getVipConfigRsp.setDuration(vipconfig.getDuration());
            getVipConfigRsp.setRight(rights);
            vipConfigRsps.add(getVipConfigRsp);
        }
        return Response.ok(vipConfigRsps);
    }

    Response publishPaging(PublishPagingReq publishPagingReq) {

        long userId = Long.parseLong(publishPagingReq.getUserId());
        long roomId = Long.parseLong(publishPagingReq.getRoomId());
        YwpdMember ywpdMember;
        YwpdRoom ywpdRoom;
        try {
            ywpdMember = ywpdMemberService.selectYwpdMemberByUserId(userId);
            ywpdRoom = ywpdRoomService.selectRoomByRoomId(roomId);
        } catch (Exception e) {
            log.error("query member failed");
            return Response.dbFail(log, "query member");
        }

        if (ywpdMember == null || ywpdRoom == null) {
            return Response.error(ErrorCode.DATA_NOT_EXIST, "用户不存在或房间不存在，请确认数据是否正确");
        }

        if (ywpdMember.getVip() != 1) {
            return Response.error(ErrorCode.NO_PERMISSION, "您还不是会员，无法发布寻人广播公告");
        }

        if (pagingService.selectByRoomId(roomId) != null) {
            Paging paging = pagingService.selectByRoomId(roomId);
            paging.setAddTime(StringUtil.getDateTime());
            pagingService.update(paging);
            return Response.ok();
        }

        Paging paging = new Paging();
        paging.setUserId(userId);
        paging.setRoomId(roomId);
        paging.setNickName(ywpdMember.getNickName());
        paging.setImgUrl(ywpdMember.getImgUrl());
        paging.setSex(ywpdMember.getSex());
        paging.setAge(ywpdMember.getAge());
        paging.setSignature(ywpdMember.getSignature());
        paging.setAddTime(StringUtil.getDateTime());

        pagingService.insert(paging);
        return Response.ok();

    }

    Response getPagingList(RequestParamBasic requestBody) {

        List<Paging> pagingList = pagingService.getAlive();
        List<Paging> pagings = new ArrayList<>();

        for (Paging paging : pagingList
        ) {
            paging.setAddTime(DateUtils.getTerm(paging.getAddTime()));
            pagings.add(paging);
        }

        return Response.ok(pagings);
    }

    Response getGoodName(GetGoodNameReq getGoodNameReq) {

        int length = getGoodNameReq.getLength();
        int pageNum = getGoodNameReq.getPageNum();
        int pageSize = getGoodNameReq.getPageSize();
        IPage<GoodName> page = new Page<>(pageNum, pageSize);
        QueryWrapper<GoodName> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GoodName::getLength, length);
        queryWrapper.lambda().eq(GoodName::getSold, 0);
        page = goodNameService.page(page, queryWrapper);
        return Response.ok(page.getRecords());
    }
}
